# -*- coding: utf-8 -*-

import sys
import pprint
# pprint.pprint(sys.path)
sys.path.append(sys.path[0]+"/../../scripts/webspider")#add module path here
# sys.path.append("D:/pyProject/SE_WORK/scripts/webspider")
# sys.path.append("D:/pyProject/SE_WORK/scripts/webspider/webspider.py")
# reload(sys)
# pprint.pprint(sys.path)
# print sys.path

from PyQt4 import QtCore, QtGui
from webspider import Parse
import thread
import time
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8

    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

lock = thread.allocate_lock()

class Ui_Form(object):
    def __init__(self):
        self.test_flag = False
        self.parse = None

        self.crawler_press = False
        self.analyze_press = False

        self.crawler_finish = True
        self.analyze_finish = True
        self.titleName = 'web_spider'

        ##创建一个线程实例并设置名称、变量、信号槽
        self.parse_URL_thread = ParseURLThread()
        self.parse_URL_thread.sinOut.connect(self.display_URL_text)

        self.parse_content_thread = ParseContentThread()
        self.parse_content_thread.sinOut.connect(self.display_content)


    #信号槽,显示解析到的网页链接
    def display_URL_text(self,s_href_list):
        if not s_href_list or s_href_list == '':
            self.textEdit.setText(_fromUtf8('Empty content:maybe your network is not good'))
        else:
            self.textEdit.setText(_fromUtf8(s_href_list))

        lock.acquire()
        self.crawler_press = False
        self.MyCrawlerButton.setEnabled(True)
        self.MyAnalyzeButton.setEnabled(True)
        self.crawler_finish = True
        lock.release()



    #信号槽,显示解析出的网页内容
    def display_content(self,s_data_list):
        if not s_data_list or s_data_list == '':
            self.textEdit.setText(_fromUtf8('Empty content:maybe your network is not good'))
        else:
            self.textEdit.setText(_fromUtf8(s_data_list))

        lock.acquire()
        self.analyze_press = False
        self.MyAnalyzeButton.setEnabled(True)
        self.MyCrawlerButton.setEnabled(True)
        self.analyze_finish = True
        lock.release()



    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8(self.titleName))
        Form.resize(1052, 727)
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(430, 10, 241, 51))
        font = QtGui.QFont()
        font.setPointSize(24)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        self.MyCrawlerButton = QtGui.QPushButton(Form)
        self.MyCrawlerButton.setGeometry(QtCore.QRect(230, 70, 171, 51))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.MyCrawlerButton.setFont(font)
        self.MyCrawlerButton.setObjectName(_fromUtf8("MyCrawlerButton"))
        self.MyAnalyzeButton = QtGui.QPushButton(Form)
        self.MyAnalyzeButton.setGeometry(QtCore.QRect(620, 70, 171, 51))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.MyAnalyzeButton.setFont(font)
        self.MyAnalyzeButton.setObjectName(_fromUtf8("MyAnalyzeButton"))
        self.textEdit = QtGui.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(150, 150, 731, 501))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.MyCrawlerButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.WebSiteDisplay)
        QtCore.QObject.connect(self.MyAnalyzeButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.DataDisplay)
        QtCore.QMetaObject.connectSlotsByName(Form)



    def WebSiteDisplay(self):
        self.textEdit.setText("WebSiteDisplay Waiting...")
        if not self.test_flag:
            url_list = ['http://www.mencius.gov.cn/news05/zhaotoubiao/zhengfucaigouzhaobiaogonggao/','http://www.mencius.gov.cn/news05/zhaotoubiao/zhengfucaigouzhaobiaogonggao/index_2.html']
            self.parse = Parse(url_list)
        else:
            test_url = 'http://www.mencius.gov.cn/news05/zhaotoubiao/zhengfucaigouzhaobiaogonggao/18612.html'
            self.parse = Parse([test_url,test_url])
        if self.analyze_press:
            self.textEdit.setText('Waiting before previous Analyzing finish')
            return

        #防止处理过程中的点击
        lock.acquire()
        if self.crawler_press:
            lock.release()
            return
        else:
            self.crawler_press = True
            self.MyCrawlerButton.setEnabled(False)
        lock.release()

        self.parse_URL_thread.setParse(self.parse)
        self.parse_URL_thread.start_parsing()

    def DataDisplay(self):
        self.textEdit.setText("DataDisplay Waiting...")
        if self.crawler_finish == False:
            self.textEdit.setText("press Crawler first!")
            return

        #防止处理过程中的点击
        lock.acquire()
        if self.analyze_press:
            lock.release()
            return
        else:
            self.analyze_press = True
            self.analyze_finish = False
            self.MyAnalyzeButton.setEnabled(False)
            self.MyCrawlerButton.setEnabled(False)
        lock.release()

        self.parse_content_thread.setParse(self.parse)
        self.parse_content_thread.start_parsing()



    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate(self.titleName, self.titleName, None))
        self.label.setText(_translate(self.titleName, "Crawler", None))
        self.MyCrawlerButton.setText(_translate(self.titleName, "Crawler", None))
        self.MyAnalyzeButton.setText(_translate(self.titleName, "Analyze", None))
        self.MyAnalyzeButton.setEnabled(False)
#避免解析url/页面内容时,界面阻塞不响应
class ParseURLThread(QtCore.QThread):

    sinOut = QtCore.pyqtSignal(str)

    def __init__(self,parent=None):
        super(ParseURLThread,self).__init__(parent)
        self.test_flag = False
        self.s_href_list = ''
        self.parse = None


    def start_parsing(self):
        ##执行线程的run方法
        self.start()

    def setParse(self,webspider_parse):
        self.parse = webspider_parse


    def run(self):
        self.parse_url_list()
        ##发射信号
        self.sinOut.emit(self.s_href_list)


    def parse_url_list(self):

        self.parse.parse_main_page()

        self.s_href_list = ''


        for i in self.parse.href_list:
            self.s_href_list += i + '\n'

        print self.parse
        print self.parse.href_list




class ParseContentThread(QtCore.QThread):

    sinOut = QtCore.pyqtSignal(str)

    def __init__(self,parent=None):
        super(ParseContentThread,self).__init__(parent)
        self.test_flag = False
        self.s_data_list = ''
        self.parse = None

    def setParse(self,webspider_parse):
        self.parse = webspider_parse

    def start_parsing(self):
        self.start()

    def run(self):
        self.parse_data_list()
        #发射信号
        self.sinOut.emit(self.s_data_list)


    def parse_data_list(self):

        self.parse.parse_child()

        # print 'writing to IR'
        self.parse.write_to_IR()
        # print 'end writing'

        self.s_data_list = ''
        for data in self.parse.res_array_all:
            for i in range(0, 3):
                self.s_data_list += data[i] + ','
            self.s_data_list += data[3] + '\n'




if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

